package com.group1.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Question;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/question")
public class QuestionWS {

	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllQuestion(){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Question> list = Question.getAllQuestion(session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			Integer questID = q.getQuestionId();
			String topicID = q.getTopic().topicName;
			String lecturerID = q.getLecturer().lecturerId;
			String questDesc = q.getQuestionDesc();
			String questType = q.getQuestionType();
			if(questType.equals("0"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("1"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("2"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			int level = q.getQuestionLevel();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"lecturerID\"" + ":\"" + lecturerID + "\",";
			result += "\"questDesc\"" + ":\"" + questDesc + "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + level+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByID/{questID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByID(@PathParam("questID") String questID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		Question q = Question.getQuestionByID(Integer.valueOf(questID), session);
		String questType = q.getQuestionType();
		if(questType.equals("0"))
		{
			questType = "Multiple Choice";
		}else{
			if(questType.equals("1"))
			{
				questType = "Fillable Blank";
			}
			else
			{
				if(questType.equals("2"))
				{
					questType = "Matching Items";
				}
				else
				{
					if(questType.equals("4"))
					{
						questType = "Full Text";
					}
					else
					{
						questType = "";
					}
				}
			}
		}
		String result;
		result = "{QUESTION:{";
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + q.getTopic().topicName + "\",";
			result += "\"lecturerID\"" + ":\"" + q.getLecturer().lecturerId + "\",";
			result += "\"questDesc\"" + ":\"" + q.getQuestionDesc() + "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + q.getQuestionLevel() + "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	@GET
	@Path("/getByTopic/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByTopic(@PathParam("topicID") String topicID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Question> list = Question.getQuestionByTopic(Integer.valueOf(topicID), session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			Integer questID = q.getQuestionId();
			String lecturerID = q.getLecturer().lecturerId;
			String questDesc = q.getQuestionDesc();
			String questType = q.getQuestionType();
			if(questType.equals("0"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("1"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("2"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			int level = q.getQuestionLevel();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + q.getTopic().getTopicName() + "\",";
			result += "\"lecturerID\"" + ":\"" + lecturerID + "\",";
			result += "\"questDesc\"" + ":\"" + questDesc + "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + level+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/getByLecturer/{lecturerID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getByLecturer(@PathParam("lecturerID") String lecturerID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<Question> list = Question.getQuestionOfLecturer(lecturerID, session);
		String result;
		result = "{QUESTION:[";
		boolean first = true;
		for (Question q : list) {
			Integer questID = q.getQuestionId();
			String topicID = q.getTopic().topicName;
			String questDesc = q.getQuestionDesc();
			String questType = q.getQuestionType();
			if(questType.equals("0"))
			{
				questType = "Multiple Choice";
			}else{
				if(questType.equals("1"))
				{
					questType = "Fillable Blank";
				}
				else
				{
					if(questType.equals("2"))
					{
						questType = "Matching Items";
					}
					else
					{
						if(questType.equals("4"))
						{
							questType = "Full Text";
						}
						else
						{
							questType = "";
						}
					}
				}
			}
			int level = q.getQuestionLevel();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"lecturerID\"" + ":\"" + lecturerID + "\",";
			result += "\"questDesc\"" + ":\"" + questDesc + "\",";
			result += "\"questType\"" + ":\"" + questType + "\",";
			result += "\"level\"" + ":\"" + level+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/create/{topicID}/{lecturerID}/{questionDesc}/{questionType}/{questionLevel}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createQuestion(@PathParam("topicID") String topicID, @PathParam("lecturerID") String lecturerID, 
			@PathParam("questionDesc") String questionDesc,@PathParam("questionType") String questionType, @PathParam("questionLevel") String questionLevel)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Question q = new Question();
				q.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				q.setLecturer(Lecturer.getLecturerById(lecturerID, session));
				q.setQuestionDesc(questionDesc);
				q.setQuestionType(questionType);
				q.setQuestionLevel(Integer.valueOf(questionLevel));
				session.save(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	
	@GET
	@Path("/update/{questionID}/{topicID}/{lecturerID}/{questionDesc}/{questionType}/{questionLevel}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateQuestion(@PathParam("questionID") String questionID,@PathParam("topicID") String topicID, @PathParam("lecturerID") String lecturerID, 
			@PathParam("questionDesc") String questionDesc,@PathParam("questionType") String questionType, @PathParam("questionLevel") String questionLevel)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Question q = Question.getQuestionByID(Integer.valueOf(questionID), session);
				q.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				q.setLecturer(Lecturer.getLecturerById(lecturerID, session));
				q.setQuestionDesc(questionDesc);
				q.setQuestionType(questionType);
				q.setQuestionLevel(Integer.valueOf(questionLevel));
				session.saveOrUpdate(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{questionID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteQuestion(@PathParam("questionID") String questionID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(Question.getQuestionByID(Integer.valueOf(questionID), session));
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
}
